<?php

namespace Console\Controller;

use Console\Common\Library\Page;

class UserController extends CstBaseController
{
    private $userLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->userLogic = A('Console/User', 'Logic');
    }

    public function lists()
    {
        $this->display();
    }

    public function doGetList()
    {
        $data = I('post.');
        $page = $data['page'] ? $data['page'] : 1;
        $pageSize = 10;
        unset($data['page']);
        if (empty($data['user_state'])) {
            $data['user_state'] = array('neq', -1);
        }

        $listRes = $this->userLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("pageHtml", $pageHtml);
        $html = $this->fetch('doGetList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));
    }

    /**
     * 获取推手设置表单
     *
     * @return void
     */
    public function doGetWorkerForm()
    {
        $id = I('post.id');
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("用户ID不能为空"));
        }
        $userInfoRes = $this->userLogic->getInfo(array(
            "id"=>$id,
            "user_state"=>1
        ));
        $userInfo = $userInfoRes["Result"];
        if (empty($userInfo)) {
            $this->ajaxReturn(static::$res->err("未找到用户信息"));
        }

        $this->assign("userInfo", $userInfo);
        $html = $this->fetch('doGetWorkerForm');

        $this->ajaxReturn(static::$res->suc("成功", $html));
    }

    /**
     * 设置为推手
     *
     * @return void
     */
    public function doSetWorker()
    {
        $id = I("post.id");
        $phone = I("post.phone");
        $pwd = I('post.password');

        $res = $this->userLogic->setWorker($id, $phone, $pwd);
        $this->ajaxReturn($res);
    }

    /**
     * 禁用推手
     *
     * @return void
     */
    public function doForbiddenWorker()
    {
        $id = I("post.id");
        
        $res = $this->userLogic->forbiddenWorker($id);
        $this->ajaxReturn($res);
    }


    /**
     * 禁用
     */
    public function doForbidden()
    {
        $id = I('post.id');

        $res = $this->userLogic->update($id, array(
            "user_state" => -2
        ));
        $this->ajaxReturn($res);
    }

    /**
     * 恢复
     */
    public function doNormal()
    {
        $id = I('post.id');

        $res = $this->userLogic->update($id, array(
            "user_state" => 1
        ));
        $this->ajaxReturn($res);
    }
}
